/**
 * 
 */
package com.nh.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.nh.entity.NgAdmin;
import com.opensymphony.xwork2.ActionContext;
import com.tdkj.util.PublicConfing;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：LoginAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-1 下午02:39:19
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-1 下午02:39:19
 * 
 * 修改备注：
 * 
 * @version
 */
public class LoginAction extends BaseAction {
	private String loginName;
	private String password;
	private String captcha;

	public String login() {
		try {
			NgAdmin admintemp = null;
			admintemp = (NgAdmin) ActionContext.getContext().getSession().get(PublicConfing.ADMINISTRATOR_ADMIN_OBJ_STRING_CONFIG);
			if (null != admintemp) {
				return "main";
			}

			this.addFieldError("validateRandom", null);
			this.addFieldError("loginPasswordError", null);
			this.addFieldError("loginNameError", null);

			String cv = (String) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_LOGIN_RANDOM);
			if (StringUtils.isEmpty(cv)) {
				ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
				this.addFieldError("validateRandom", "验证码不正确,请重新输入");
				return "fail";
			}
			if ("".equals(captcha) || !captcha.equals(cv)) {
				ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
				this.addFieldError("validateRandom", "验证码不正确,请重新输入");
				return "fail";
			}
			List<NgAdmin> list = this.getBaseService().findByProperty("loginName", loginName, NgAdmin.class);
			if (list.size() > 0) {
				NgAdmin admin = list.get(0);
				if (password.equals(admin.getLoginPassword())) {
					ActionContext.getContext().getSession().put(PublicConfing.ADMINISTRATOR_ADMIN_OBJ_STRING_CONFIG, admin);
					loginName = "";
					password = "";

					// 重定向
					if (null != ActionContext.getContext().getSession().get(PublicConfing.REDIRECTURL_TEMP)) {
						String url = (String) ActionContext.getContext().getSession().get(PublicConfing.REDIRECTURL_TEMP);
						// System.out.println("登录得到地址：" + url);
						HttpServletResponse response = ServletActionContext.getResponse();
						try {
							ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
							response.sendRedirect(url);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return null;
					}

					return "main";
				}
				ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
				this.addFieldError("loginPasswordError", "密码不正确，请重新输入");
				return "fail";
			}
			ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
			this.addFieldError("loginNameError", "用户名不正确，请重新输入");
		} catch (Exception e) {
			e.printStackTrace();
		}
		ActionContext.getContext().getSession().put(PublicConfing.REDIRECTURL_TEMP, null);
		return "fail";
	}

	/**
	 * 
	 * @return
	 */
	public String longOut() {
		ActionContext.getContext().getSession().put(PublicConfing.ADMINISTRATOR_ADMIN_OBJ_STRING_CONFIG, null);
		return "fail";
	}

	/**
	 * @return the loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName
	 *            the loginName to set
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the captcha
	 */
	public String getCaptcha() {
		return captcha;
	}

	/**
	 * @param captcha
	 *            the captcha to set
	 */
	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

}
